<template>
  <div class="banner-add">

    <div class="createPost-main-container">
      <el-form ref="form" label-width="120px" :rules="rules" :model="form">

        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" type="text" maxlength="20" show-word-limit />
        </el-form-item>

        <el-form-item label="主图" prop="imgData">
          <upload :data="imgData" />
        </el-form-item>

        <!-- <el-form-item label="资讯摘要" prop="summary">
          <el-input v-model="form.summary" type="text" maxlength="100" show-word-limit />
        </el-form-item> -->

        <el-form-item label="详情" prop="content">
          <tinymce-editor
            ref="myEditor"
            upload-model="news"
            show-hyperlink-btn
          />

        </el-form-item>

      </el-form>
    </div>

    <sticky :class-name="'cms-sub-navbar '+ajaxStatus">
      <el-button :loading="ajaxStatus" style="margin-left: 10px;" type="success" @click="submitForm('1')">
        发布
      </el-button>
    </sticky>

  </div>
</template>

<script>
import { postData, getEditData } from '@/api/info/index'
import Sticky from 'components/Sticky'
import Upload from 'components/Upload/img-upload'
import TinymceEditor from 'components/ueditor'

// /others 跟news传参一样，加个otherType ,值取(
// STANDER(量化标准),SERVICE（服务）,ABOUT_US（关于我们）这三个

const defaultForm = {
  title: '实例1',
  picture: '',
  summary: 'summary',
  otherType: null,
  content: '<p>details</p>'
}

export default {
  components: { Sticky, Upload, TinymceEditor },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    tabName: {
      type: String,
      default: ''
    }
  },
  data() {
    const checkContent = (rule, value, callback) => {
      if (!this.form.content) {
        return callback(new Error('请填入资讯正文'))
      }
      callback()
    }
    const checkImg = (rule, value, callback) => {
      if (!this.imgData.imgUrl) {
        return callback(new Error('请上传图片'))
      }
      callback()
    }
    return {
      ajaxStatus: false,
      form: Object.assign({}, defaultForm),

      imgData: {
        myId: Math.random(),
        path: 'news',
        imgUrl: '',
        ImgSize: 10000,
        text: '图片大小不得超过10000K，支持png/jpg格式'
      },

      rules: {
        title: { required: true, message: '请输入小于20个字的标题', trigger: 'blur', min: 1, max: 20 },
        // summary: { required: true, message: '请输入小于20个字的资讯摘要', trigger: 'blur', min: 1, max: 100 },
        imgData: { required: true, validator: checkImg },
        content: { required: true, validator: checkContent }
      }
    }
  },
  computed: {
  },
  created() {
    this.form.otherType = this.$route.name
    this.fetchData()
  },
  methods: {

    // 获取编辑数据
    fetchData() {
      getEditData({
        type: this.form.otherType
      }).then(re => {
        const { data } = re
        if (data) {
          console.log('data', data)
          this.form = Object.assign(this.form, data)
          this.imgData.imgUrl = this.form.picture
          setTimeout(() => {
            console.log('data.content', data.content)
            this.$refs.myEditor.edit(data.content)
          }, 500)
        }
      })
    },

    /**
     * 保存、发布接口
     *
     * id：保存为空，编辑传列表id
     */
    submitForm(status) {
      if (this.ajaxStatus) {
        return
      }
      this.ajaxStatus = true
      this.form.picture = this.imgData.imgUrl
      this.form.content = this.$refs.myEditor.onEditorChange()
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.status = status
          postData(this.form)
            .then(re => {
              this.ajaxStatus = false
              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
            })
            .catch(() => {
              this.ajaxStatus = false
            })
        } else {
          this.ajaxStatus = false
          this.$message.error('请正确填写完整信息后再发布')
          return false
        }
      })
    }

  }
}
</script>
<style lang="scss" scoped>
.href-remark{
  color: #5e6d82;
  .red {
    background-color: #fdf6ec;
    color: #e6a23c;
  }
}
.logo-card{
  margin-bottom: 10px;
}
</style>

